<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Action</title>
  </head>
  <body>
    <div id="app">
      <p>count:{{count}}</p>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="https://unpkg.com/vuex@3.1.3/dist/vuex.js"></script>
    <script>
      var store = new Vuex.Store({
        state: { count: 1 },
        mutations: {
          increase(state) {
            //此处只能放同步代码，不能放异步代码
            state.count++;
          },
        },
        actions: {
          increaseInAction(context) {
            //context=store
            context.commit("increase");
          },
          increaseAsyncInAction(context) {
            //context=store
            setTimeout(() => {
              context.commit("increase");
            }, 1000);
          },
        },
      });
      console.log(store.state.count);
      //store.commit('increase');
      //调用dispatch方法可以派发 action
      //store.dispatch("increaseInAction");
      store.dispatch("increaseAsyncInAction");
      console.log(store.state.count);
      setTimeout(() => {
        console.log(store.state.count);
      }, 2000);
    </script>
  </body>
</html>
